﻿Imports System.Security.Cryptography
Imports System.IO
Imports System.Runtime.Serialization.Formatters
Imports System.Xml.Serialization
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms


''' <summary>
''' Stored many common functions useful
''' </summary>
''' <remarks></remarks>
Public Module PublicFunction

#Region "Encryption Password MD5"
    Public Function EncryptPassword(ByVal Password As String) As String

        'Encrypt the Password       
        Dim sEncryptedPassword As String = ""
        Dim sEncryptKey As String = "P@SSW@RD@09"        'Should be minimum 8 characters       
        Try

            sEncryptedPassword = EncryptDecryptClass.EncryptPasswordMD5(Password, sEncryptKey)
        Catch ex As Exception
            Return sEncryptedPassword
        End Try
        Return sEncryptedPassword
    End Function
    Public Function DecryptPassword(ByVal Password As String) As String

        'Encrypt the Password       
        Dim sDecryptedPassword As String = ""
        Dim sEncryptKey As String = "P@SSW@RD@09" 'Should be minimum 8 characters      
        Try
            sDecryptedPassword = EncryptDecryptClass.DecryptPasswordMD5(Password, sEncryptKey)
        Catch ex As Exception
            Return sDecryptedPassword
        End Try
        Return sDecryptedPassword
    End Function



#End Region

#Region "Serialize, Deserialize"
    Public Sub BinarySerialize(ByVal filename As String, ByVal obj As Object)

        Dim fileStreamObject As FileStream

        fileStreamObject = New FileStream(filename, FileMode.Create)
        Dim binaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        binaryFormatter.Serialize(fileStreamObject, obj)
        fileStreamObject.Close()
    End Sub
    Public Function BinaryDeserialize(ByVal filename As String) As Object

        Dim fileStreamObject As New FileStream(filename, FileMode.Open)
        Dim binaryFormatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
        Dim obj As Object = binaryFormatter.Deserialize(fileStreamObject)
        fileStreamObject.Close()
        Return obj
    End Function
    Public Sub SoapSerialize(ByVal filename As String, ByVal obj As Object)

        Dim fileStreamObject As FileStream

        fileStreamObject = New FileStream(filename, FileMode.Create)
        Dim binaryFormatter As New Runtime.Serialization.Formatters.Soap.SoapFormatter()
        binaryFormatter.Serialize(fileStreamObject, obj)
        fileStreamObject.Close()
    End Sub
    Public Function SoapDeserialize(ByVal filename As String) As Object

        Dim fileStreamObject As New FileStream(filename, FileMode.Open)
        Dim binaryFormatter As New Runtime.Serialization.Formatters.Soap.SoapFormatter()
        Dim obj As Object = binaryFormatter.Deserialize(fileStreamObject)
        fileStreamObject.Close()
        Return obj
    End Function
    Public Sub XMLSerialize(ByVal filename As String, ByVal obj As Object)

        Dim fileStreamObject As FileStream
        fileStreamObject = New FileStream(filename, FileMode.Create)
        Dim binaryFormatter As New XmlSerializer(obj.GetType)
        binaryFormatter.Serialize(fileStreamObject, obj)
        fileStreamObject.Close()
    End Sub
    Public Function XMLDeserialize(ByVal filename As String, ByVal obj As Object) As Object

        Dim fileStreamObject As New FileStream(filename, FileMode.Open)
        Dim binaryFormatter As New XmlSerializer(obj.GetType)
        Dim o As Object = binaryFormatter.Deserialize(fileStreamObject)
        fileStreamObject.Close()
        Return o
    End Function


#End Region

#Region "File and Folder "
    Function ConvertFileToArrayByte(ByVal filename As String) As Byte()
        Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
        'Create a byte array of file stream length  
        Dim ImageData(fs.Length) As Byte
        'Read block of bytes from stream into the byte array  
        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length))
        'Close the File Stream   fs.Close();   return ImageData; 
        Return ImageData
    End Function

    Sub ConvertArrayByteToImage()



    End Sub

#End Region

#Region "Add char, string "
    Public Function AddLeft(ByVal charAdd As String, ByVal Value As String, ByVal lenght As Integer) As String
        For index As Integer = 1 To lenght - Value.Length
            Value = charAdd + Value
        Next
        Return Value
    End Function
    Public Function AddRight(ByVal charAdd As String, ByVal Value As String, ByVal lenght As Integer) As String
        For index As Integer = 1 To lenght - Value.Length
            Value = Value + charAdd
        Next
        Return Value
    End Function
#End Region

#Region "Drawing"
    Sub FillControl(ByVal X As Integer, ByVal Y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim rectange As New Rectangle(X, Y, width, height)
        Dim rectangeDraw As New Rectangle(X, Y, width - 2, height - 2)
        Dim brush As New LinearGradientBrush(rectange, Color.White, Color.YellowGreen, LinearGradientMode.Horizontal)
        Dim pen As New Pen(PublicConst.Color_Boder, 2)
        e.Graphics.FillRectangle(brush, rectange)
        e.Graphics.DrawRectangle(pen, rectangeDraw)
    End Sub
    Sub FillForm(ByVal X As Integer, ByVal Y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim rectange As New Rectangle(X, Y, width, height)
        Dim rectangeDraw As New Rectangle(X, Y, width - 2, height - 2)
        Dim brush As New LinearGradientBrush(rectange, Color.White, Color.YellowGreen, LinearGradientMode.Vertical)
        Dim pen As New Pen(PublicConst.Color_Boder, 2)
        e.Graphics.FillRectangle(brush, rectange)
        e.Graphics.DrawRectangle(pen, rectangeDraw)
    End Sub
    Sub FillControl(ByVal X As Integer, ByVal Y As Integer, ByVal width As Integer, ByVal height As Integer, ByVal color1 As Color, ByVal color2 As Color, ByVal lineMode As LinearGradientMode, ByVal e As System.Windows.Forms.PaintEventArgs)
        Dim rectange As New Rectangle(X, Y, width, height)
        Dim rectangeDraw As New Rectangle(X, Y, width - 2, height - 2)
        Dim brush As New LinearGradientBrush(rectange, color1, color2, lineMode)
        Dim pen As New Pen(PublicConst.Color_Boder, 2)
        e.Graphics.FillRectangle(brush, rectange)
        e.Graphics.DrawRectangle(pen, rectangeDraw)
    End Sub
#End Region

#Region "Set color"
    Public Sub SetColorEnter(ByVal textBox As TextBox)
        textBox.BackColor = PublicConst.Color_Entry_Text
        textBox.SelectAll()
    End Sub
    Sub SetColorLeave(ByVal textBox As TextBox)
        textBox.BackColor = PublicConst.Color_Leave_Text
    End Sub
    Public Sub SetColorEnter(ByVal combo As ComboBox)
        combo.BackColor = PublicConst.Color_Entry_Text
    End Sub
    Sub SetColorLeave(ByVal combo As ComboBox)
        combo.BackColor = PublicConst.Color_Leave_Text
    End Sub


#End Region

#Region "Show message"
    Function ShowQuestion(ByVal message As String, ByVal Caption As String) As DialogResult
        Return MessageBox.Show(message, Caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    End Function
    Function ShowQuestionDelete() As DialogResult
        Return MessageBox.Show(PublicConst.Message_Caption_Delete, PublicConst.Message_Caption_Confirm, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    End Function
    Function ShowQuestionSave() As DialogResult
        Return MessageBox.Show(PublicConst.Message_Question_Save, PublicConst.Message_Caption_Confirm, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    End Function
    Function ShowQuestionImport() As DialogResult
        Return MessageBox.Show(PublicConst.Message_Question_Import, PublicConst.Message_Caption_Confirm, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
    End Function

    Function ShowError(ByVal message As String, ByVal Caption As String)
        Return MessageBox.Show(message, Caption, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Function
#End Region

#Region "Create ID"
    Public Function IncreaseIndex(ByVal index As String, ByVal lenght As Integer) As String
        If index.Length > 0 Then
            Dim value As Integer = CInt(index)
            value += 1
            Return AddLeft("0", value, lenght)
        End If
        Return AddLeft("0", 1, lenght)
    End Function
#End Region


End Module
